﻿@model IEnumerable<IniTechCaash.WebApplication.Models.FinanceAccountDetailsViewModel>
@using IniTechCaash.Domain.Entities

@{
    ViewBag.Title = "Ratio Analysis";

    decimal currentAssets = 0.00m;
    decimal quickAssets = 0.00m;
    decimal currentLiabilities = 0.00m;
    decimal longLiabilities = 0.00m;
    decimal totalLiabilities = 0.00m;
    if (Model.Where(a => a.Type == AccountType.CurrentAsset || a.Type == AccountType.InventoryAsset).Count() > 0)
    {
        currentAssets = Model.Where(a => a.Type == AccountType.CurrentAsset || a.Type == AccountType.InventoryAsset).Select(a => a.Balance).Aggregate((a, b) => a + b);
    }
    if (Model.Where(a => a.Type == AccountType.CurrentAsset).Count() > 0)
    {
        quickAssets = Model.Where(a => a.Type == AccountType.CurrentAsset).Select(a => a.Balance).Aggregate((a, b) => a + b);
    }
    if (Model.Where(a => a.Type == AccountType.CurrentLiability).Count() > 0)
    {
        currentLiabilities = Model.Where(a => a.Type == AccountType.CurrentLiability).Select(a => a.Balance).Aggregate((a, b) => a + b);
    }
    if (Model.Where(a => a.Type == AccountType.LongTermLiability).Count() > 0)
    {
        longLiabilities = Model.Where(a => a.Type == AccountType.LongTermLiability).Select(a => a.Balance).Aggregate((a, b) => a + b);
    }
    totalLiabilities = longLiabilities + currentLiabilities;

    var assetAccounts = Model.Where(a => a.Type.GetGeneralAccountType() == GeneralAccountType.Asset);
    decimal totalAssets = 0.00m;
    foreach (var a in assetAccounts)
    {
        totalAssets += a.Balance;
    }

    decimal salesRevenue = 0.00m;
    foreach (var a in Model.Where(a => a.Type == AccountType.SalesRevenue || a.Type == AccountType.ServiceRevenue))
    {
        salesRevenue += a.Balance;
    }

    var revenueAccounts = Model.Where(a => a.Balance != 0.00m).Where(a => a.Type.GetGeneralAccountType() == GeneralAccountType.Revenue);
    decimal grossProfit = 0.00m;
    foreach (var a in revenueAccounts)
    {
        grossProfit += a.Balance;
    }

    var expenseAccounts = Model.Where(a => a.Balance != 0.00m).Where(a => a.Type.GetGeneralAccountType() == GeneralAccountType.Expense);
    decimal totalExpenses = 0.00m;
    foreach (var a in expenseAccounts)
    {
        totalExpenses += a.Balance;
    }
    decimal netIncome = salesRevenue - totalExpenses;

    var equityAccounts = Model.Where(a => a.Type.GetGeneralAccountType() == GeneralAccountType.Equity);
    decimal totalEquities = 0.00m;
    foreach (var a in equityAccounts)
    {
        totalEquities += a.Balance;
    }
}

<div>
    <h3>IniTech</h3>
    <h4>Ratio Analysis</h4>
    <p>As of @DateTime.Now.ToString("MMM ddd d HH:mm yyyy")</p>
</div>

<hr />

<h4>Liquidity</h4>
<dl>
    <dt>Current Ratio</dt>
    <dd>@(currentLiabilities == 0 ? "0.00%" : (currentAssets / currentLiabilities).ToString("P"))</dd>
    <dt>Quick Ratio</dt>
    <dd>@(currentLiabilities == 0 ? "0.00%" : (quickAssets / currentLiabilities).ToString("P"))</dd>
    <dt>Net Working Capital to Sales Ratio</dt>
    <dd>@(salesRevenue == 0 ? "0.00%" : ((currentAssets - currentLiabilities) / salesRevenue).ToString("P"))</dd>
</dl>

<h4>Profitability</h4>
<dl>
    <dt>Gross Profit Margin</dt>
    <dd>@(salesRevenue == 0 ? "0.00%" : (grossProfit / salesRevenue).ToString("P"))</dd>
    <dt>Operating Profit Margin</dt>
    <dd>@(salesRevenue == 0 ? "0.00%" : (netIncome / salesRevenue).ToString("P"))</dd>
    <dt>Net Profit Margin</dt>
    <dd>@(salesRevenue == 0 ? "0.00%" : (netIncome / salesRevenue).ToString("P"))</dd>
</dl>

<h4>Financial Leverage</h4>
<dl>
    <dt>Total Debt to Assets</dt>
    <dd>@(totalAssets == 0 ? "0.00%" : (totalLiabilities / totalAssets).ToString("P"))</dd>
    <dt>Long Term Debt to Assets</dt>
    <dd>@(totalAssets == 0 ? "0.00%" : (longLiabilities / totalAssets).ToString("P"))</dd>
    <dt>Total Debt to Equity</dt>
    <dd>@(totalEquities == 0 ? "0.00%" : (totalLiabilities / totalEquities).ToString("P"))</dd>
    <dt>Equity Multiplier</dt>
    <dd>@(totalEquities == 0 ? "0.00%" : (totalAssets / totalEquities).ToString("P"))</dd>
</dl>

<h4>Return</h4>
<dl>
    <dt>Basic Earning to Power</dt>
    <dd>@(totalAssets == 0 ? "0.00%" : (netIncome / totalAssets).ToString("P"))</dd>
    <dt>Return on Assets</dt>
    <dd>@(totalAssets == 0 ? "0.00%" : (netIncome / totalAssets).ToString("P"))</dd>
    <dt>Return on Equity</dt>
    <dd>@(totalEquities == 0 ? "0.00%" : (netIncome / totalEquities).ToString("P"))</dd>
</dl>

<hr />

@Html.ActionLink("Back To List", "Index")
    